<?php
declare (strict_types = 1);

namespace app\admin\controller;

use bbweb\basic\BaseController;
use app\admin\model\system\SystemAdmin;
use app\Request;
use bbweb\services\UtilService;
use think\facade\Config;
use think\facade\Route as Url;
use think\facade\Session;

/**
 * 登录验证控制器
 * Class Login
 * @package app\admin\controller
 */
class Login extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return string
     */
    public function index(Request $req)
    {
        if($req->isPost()) {
            list($account, $password, $verify) = UtilService::postMore([
                'account', 'password', 'verify'
            ], $req, true);
            //检验验证码
            if (!captcha_check($verify)) $this->error('验证码错误，请重新输入');
            $error = Session::get('login_error') ?: ['num' => 0, 'time' => time()];
            $error['num'] = 0;
            if ($error['num'] >= 5 && $error['time'] > strtotime('- 5 minutes'))
                $this->error('错误次数过多,请稍候再试!');
            //检验帐号密码
            $res = SystemAdmin::login($account, $password);
            if ($res) {
                Session::set('login_error', null);
                Session::save();
                return $this->success('登陆成功、跳转中…', '/index/index');
            } else {
                $error['num'] += 1;
                $error['time'] = time();
                Session::set('login_error', $error);
                Session::save();
                $this->error(SystemAdmin::getErrorInfo('用户名错误，请重新输入'));
            }
        }
        return $this->fetch();
    }

    public function captcha()
    {
        ob_clean();
        return captcha();
    }

    /**
     * 锁屏解锁
     */
    public function unlocked(Request $req)
    {
        // 执行登录
        if ($req->isPost()) {
            // 获取密码
            $password = $req->param('password');
            $adminInfo = SystemAdmin::activeAdminInfoOrFail();
            if(!$adminInfo) {
                $this->error(SystemAdmin::getErrorInfo('请登陆!'), Url::buildUrl('login/index')->build());
            }
            // 对比账号
            if ($adminInfo['level'] === 0 ) {
                if( $password == Config::get('hello.admin.password')) {
                    $this->success('验证成功！');
                } else {
                    $this->error();
                }
                // 登录成功
            } else {
                // 获取账号
                $sa = SystemAdmin::get($adminInfo['id']);
                if (empty($sa) || $sa['password'] != md5(md5($password).Config::get('hello.secret')) ) {
                    $this->error('账号或密码错误！');
                    exit;
                }
                // 是否停用
                if (!$sa['status']) {
                    $this->error('您的账号已被停用！');
                    exit;
                }
                $this->success('验证成功！');
                exit;
            }
        }
    }

    /**
     * 退出登陆
     */
    public function logout()
    {
        SystemAdmin::clearLoginInfo();
        $this->success('安全退出', '/login/index');
    }

    /**
     * 判断登陆
     */
    public function check_status() {
        // 登录超时
        if ( !SystemAdmin::hasActiveAdmin()) {
            $this->error('掉线');
        }
        $this->success('在线');
    }
}
